# -*-coding:utf-8 -*-

from content.models import HowToGo, Content, News, Notice, Notify, OfficeDoc, Video, Picture, SubPicture, ThirdParty
from rest_framework import serializers
from core.api.serializers import DynamicFieldsSerializerMixin
from django.contrib.auth.models import User
from rest_framework.reverse import reverse


class HowToGoSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)

    class Meta:
        model = HowToGo
        fields = '__all__'



class NewsSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)

    class Meta:
        model = News
        fields = '__all__'


class NoticeSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)

    class Meta:
        model = Notice
        fields = '__all__'


class NotifySerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)

    class Meta:
        model = Notify
        fields = '__all__'


class OfficeDocSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)

    class Meta:
        model = OfficeDoc
        fields = '__all__'


class VideoSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)

    class Meta:
        model = Video
        fields = '__all__'


class SubPictureSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)

    class Meta:
        model = SubPicture
        fields = '__all__'


class PictureSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)
    subpictures = SubPictureSerializer(many=True,)

    class Meta:
        model = Picture
        fields = '__all__'


class ContentSerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):

    __str__ = serializers.CharField(read_only=True)
    department_name = serializers.URLField(source='get_department_name', read_only=True)
    user_name = serializers.URLField(source='get_user_name', read_only=True)
    detail_api = serializers.SerializerMethodField()

    def get_detail_api(self, obj):
        request = self.context["request"]
        
        if obj.category == Content.CAT_NEWS:
            return request.build_absolute_uri(reverse("api:news-detail", args=[obj.id]))  # "/api/content/news/%d.html" % obj.id
        elif obj.category == Content.CAT_NOTICE:
            return request.build_absolute_uri(reverse("api:notice-detail", args=[obj.id]))  # "/api/content/notice/%d.html" % obj.id
        elif obj.category == Content.CAT_NOTIFY:
            return request.build_absolute_uri(reverse("api:notify-detail", args=[obj.id]))  # "/api/content/notify/%d.html" % obj.id
        elif obj.category == Content.CAT_OFFICE:
            return request.build_absolute_uri(reverse("api:officedoc-detail", args=[obj.id]))  # "/api/content/officedoc/%d.html" % obj.id
        elif obj.category == Content.CAT_VIDEO:
            return request.build_absolute_uri(reverse("api:video-detail", args=[obj.id]))  # "/api/content/video/%d.html" % obj.id
        elif obj.category == Content.CAT_PICTURE:
            return request.build_absolute_uri(reverse("api:picture-detail", args=[obj.id]))  # "/api/content/picture/%d.html" % obj.id
        else:
            return None

    class Meta:
        model = Content
        fields = '__all__'


class ThirdPartySerializer(DynamicFieldsSerializerMixin, serializers.ModelSerializer):
    '''
    第三方内容序列化
    '''
    __str__ = serializers.CharField(read_only=True)
    # method_filed = serializers.CharField(source='model_function_name', read_only=True) # only example, please modify.
    # children = ChildrenModelSerializer(many=True, read_only=True) # only example, please modify.

    class Meta:
        model = ThirdParty
        fields = '__all__'
